package com.example.eureka_client.controller;


import com.example.eureka_client.entity.AttentionUser;
import com.example.eureka_client.entity.UacRole;
import com.example.eureka_client.entity.UacUser;
import com.example.eureka_client.httpResult.Result;
import com.example.eureka_client.httpResult.ResultCode;
import com.example.eureka_client.httpResult.ResultGenerator;
import com.example.eureka_client.service.IAttentionUserService;
import com.example.eureka_client.service.IUacRoleService;
import com.example.eureka_client.service.IUacUserService;
import com.example.eureka_client.utils.PageRequest;
import com.example.eureka_client.utils.PageUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.RegEx;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author plj
 * @since 2022-03-24
 */
@Controller
@RestController
@Api(description = "用户接口 ")
@RequestMapping("/uac-user")
public class UacUserController {

    @Value("${Dir}")
    String Dir;

    @Resource
    IUacUserService iUacUserService;

    @Resource
    IUacRoleService iUacRoleService;

    @Resource
    IAttentionUserService iAttentionUserService;

    @ApiOperation(value = "注册")
    @PostMapping("register")
    public Result insert(@RequestBody UacUser uacUser){
        UacRole uacRole = iUacRoleService.getByName("普通用户");
        Date date = new Date();

        uacUser.setId(UUID.randomUUID().toString());
        uacUser.setPassword(DigestUtils.md5DigestAsHex(uacUser.getPassword().getBytes()));
        uacUser.setCreateDate(date);
        uacUser.setRoleId(uacRole.getId());
        if(uacUser.getPhoto() == null) uacUser.setPhoto("/image/" + "photo.jpg");

        boolean flag = iUacUserService.save(uacUser);
        if(flag) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("用户创建失败");
    }

    @PostMapping("uplodeImage")
    public Result uploadImage(@RequestPart(value = "file") MultipartFile upFile){
        String name = upFile.getOriginalFilename();
        String type = name.split("\\.")[1];

        String fileName = UUID.randomUUID().toString() + "." + type;
        File Image = new File(Dir +  fileName);
        try {
            if(!Image.exists())
                Image.createNewFile();
            upFile.transferTo(Image);
            return ResultGenerator.genSuccessResult("/image/" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("头像上传失败");
        }
    }

    @PostMapping("login")
    public Result login(@RequestBody UacUser uacUser){
        UacUser realUser = iUacUserService.getByLoginName(uacUser.getLoginName());
        String password = DigestUtils.md5DigestAsHex(uacUser.getPassword().getBytes());

        if(realUser == null || realUser.getDelFlag() == 1 || !password.equals(realUser.getPassword())) return ResultGenerator.genFailResult(ResultCode.USERNAME_OR_PASSWORD_ERROR);
        else return ResultGenerator.genSuccessResult(realUser);
    }

    @ApiOperation(value = "用户关注其他用户")
    @PostMapping("addAttention")
    public Result addAttention(@RequestBody AttentionUser attentionUser){
        if(iAttentionUserService.save(attentionUser)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("关注失败");
    }

    @ApiOperation(value = "用户取消关注其他用户")
    @PostMapping("reAttention")
    public Result reAttention(@RequestParam String userId, @RequestParam String toId){
        if(iAttentionUserService.delAttention(userId, toId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("取消关注失败");
    }

    @ApiOperation(value = "获取用户的关注列表， 返回分页结果")
    @GetMapping("getAttentionUserList")
    public Result getAttentionUserList(@RequestParam String userId, @RequestParam int pageNum, @RequestParam int pageSize){
        List<String> attentionUsers = iAttentionUserService.getAllByUserId(userId);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        if(attentionUsers.size() != 0)
            return ResultGenerator.genSuccessResult(iUacUserService.getUserList(attentionUsers, pageRequest));
        else return ResultGenerator.genSuccessResult(new ArrayList<String>(){{add(null);}});
    }

    @ApiOperation(value = "获取用户的粉丝列表， 返回分页结果")
    @GetMapping("getFensList")
    public Result getFensList(@RequestParam String userId,@RequestParam int pageNum, @RequestParam int pageSize){
        List<String> fensList = iAttentionUserService.getAllByToId(userId);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        if(fensList.size() != 0) {
            return ResultGenerator.genSuccessResult(iUacUserService.getUserList(fensList, pageRequest));
        } else return ResultGenerator.genSuccessResult(new ArrayList<String>(){{add(null);}});
    }

}
